package com.yc.common.handler;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.servlet.HandlerInterceptor;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.Objects;

/**
 * @program: lore_quiz
 * @description: 自定义烂拦截器
 * @author: 作者 huchaojie
 * @create: 2023-03-23 16:36
 */
public class Interceptor implements HandlerInterceptor {

    @Resource
    private RedisTemplate redisTemplate;

    @Value("${server.servlet.context-path}")
    private String contextPath;

    /**
     * 在请求处理之前进行调用（Controller方法调用之前）
     */
    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) {
        try {
            if(request.getSession() != null ){
                String sessionId = request.getSession().getId();
                if(request.getSession().getAttribute("userID") != null){
                    String userid = (String) request.getSession().getAttribute("userID");
                    Object object = redisTemplate.opsForHash().get("USERLOGINTOKEN",userid);
                    if(Objects.isNull(object) && request.getRequestURI().indexOf("login")>0){
                        return true;
                    }

                    if(!sessionId.equals(String.valueOf(object))){
                        response.sendRedirect("/"+contextPath+"/otherlogin");
                        return false;
                    }
                }
            }
            Object loginUser = request.getSession().getAttribute("user");
            if(Objects.isNull(loginUser)){
                //response.sendRedirect("/"+contextPath+"/nologin");
                //return false;
                return true;
            }
            //这里设置拦截以后重定向的页面，一般设置为登陆页面地址
        } catch (Exception e) {
            e.printStackTrace();
        }
        return true;
        //如果设置为false时，被请求时，拦截器执行到此处将不会继续操作
        //如果设置为true时，请求将会继续执行后面的操作
    }

}
